package com.myrslok.util;

import org.jetbrains.annotations.NotNull;

import java.util.Iterator;

public class StringUtil {
    @NotNull
    public static <T> String join(@NotNull final String separator, @NotNull final Iterable<T> values) {
        return join(separator, values, new NormalStringifier());
    }

    public static <T> String join(@NotNull final String separator, @NotNull final Iterable<T> values, @NotNull final Stringifier<T> stringifier) {
        final StringBuilder sb = new StringBuilder();
        for (Iterator<T> i=values.iterator(); i.hasNext();) {
            final T value = i.next();
            sb.append(stringifier.toString(value));
            if (i.hasNext()) {
                sb.append(separator);
            }
        }
        return sb.toString();
    }

    public static interface Stringifier<T> {
        public String toString(@NotNull final T t);
    }

    private static class NormalStringifier implements Stringifier {
        @Override
        public String toString(@NotNull Object o) {
            return o.toString();
        }
    }
}
